package com.tang.mybatis.binding;

import com.tang.mybatis.session.SqlSession;

import java.lang.reflect.Proxy;

/**
 * @author Tang
 * @classname MapperProxyFactory
 * @description [  ]
 * @date 2022/4/6 23:45
 */
public class MapperProxyFactory<T> {

    private Class<T> mapperInterface;

    public MapperProxyFactory(Class<T> mapperInterface) {
        this.mapperInterface = mapperInterface;
    }

    public <T> T newInstance(SqlSession sqlSession) {
        MapperProxy<T> mapperProxy = new MapperProxy(this.mapperInterface, sqlSession);
        return newInstance(mapperProxy);
    }

    public <T> T newInstance(MapperProxy<T> mapperProxy) {
        return (T) Proxy.newProxyInstance(this.mapperInterface.getClassLoader(),
                new Class[]{mapperProxy.getMapperInterface()},
                mapperProxy);
    }
}